package com.supcalls;

import java.util.concurrent.atomic.AtomicMarkableReference;

public class MarkableCAS {
    static AtomicMarkableReference<String> ref = new AtomicMarkableReference<>("A", true);

    public static void main(String[] args) throws InterruptedException {
        System.out.println("main start...");
        // 获取值 A
        String prev = ref.getReference();
        other();
        Thread.sleep(1000);
        // 尝试改为 C
        System.out.println("change A->C {}" + ref.compareAndSet(prev, "C", true, false));
    }

    private static void other() {
        new Thread(() -> {
            System.out.println("change A->B {}" + ref.compareAndSet(ref.getReference(), "B", true, false));
        }, "t1").start();
    }
}
